package com.legal.controller.chat;

import cn.hutool.core.bean.BeanUtil;

import com.legal.domain.dto.CasesDTO;
import com.legal.domain.po.Cases;
import com.legal.domain.result.Result;
import com.legal.domain.vo.CasesVO;
import com.legal.service.chat.ICasesService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Comparator;
import java.util.List;

@RequiredArgsConstructor
@RestController
@RequestMapping("/cases")
@Slf4j
@Tag(name = "案件基本信息接口")
public class CasesController {

    private final ICasesService casesService;


    @Operation(summary = "根据userNO和chatId获取案件信息")
    @PostMapping(value = "/detail")
    public Result<CasesVO> one(@RequestParam("userNo") String userNo, @RequestParam("chatId") String chatId){
        Cases cases = casesService.getCases(userNo, chatId);
        CasesVO casesVO = BeanUtil.copyProperties(cases, CasesVO.class);
        return Result.ok(casesVO);
    }

    @Operation(summary = "根据id获取案件信息")
    @GetMapping(value = "/getById/{id}")
    public Result<CasesVO> getById(@PathVariable("id") Integer id){
        Cases cases = casesService.getById(id);
        CasesVO casesVO = BeanUtil.copyProperties(cases, CasesVO.class);
        return Result.ok(casesVO);
    }

    @Operation(summary = "根据userNo获取所有的案件信息")
    @PostMapping(value = "/list")
    public Result<List<CasesVO>> all(@RequestParam("userNo") String userNo){
        log.info("获取案件信息");
        List<CasesVO> casesVOS = casesService
                .getAllCasesByUserNo(userNo)
                .stream()
                .map(cases -> BeanUtil.copyProperties(cases, CasesVO.class))
                .sorted(Comparator.comparing(CasesVO::getUpdateTime).reversed())//根据修改时间即updateTime进行排序,按时间降序
                .toList();
        return Result.ok(casesVOS);
    }

    /**
     * 添加案件
     * @param casesDTO 需要 userNo,caseName,caseProcedure
     * @return
     */
    @Operation(summary = "添加案件")
    @PostMapping(value = "/add")
    public Result<Void> addCase(@RequestBody CasesDTO casesDTO){
        casesService.addCase(casesDTO);
        return Result.ok();
    }

    /**
     * 修改案件内容 案件名字和案件程序阶段
     * @param casesDTO
     */
    @Operation(summary = "修改案件内容 案件名字和案件程序阶段")
    @PostMapping(value = "/update")
    public Result<Void> updateCase(@RequestBody CasesDTO casesDTO){
        casesService.updateCaseById(casesDTO);
        return Result.ok();
    }
    /**
     * 根据id删除案件
     * @param id
     */
    @Operation(summary = "根据id删除案件")
    @DeleteMapping(value = "/delete/{id}")
    public Result<Void> deleteCaseById(@PathVariable("id") Integer id){
        casesService.removeById(id);
        return Result.ok();
    }






}
